        //
//  CBJViewController.m
//  中关村在线
//
//  Created by hy on 16/1/9.
//  Copyright © 2016年 hongyu. All rights reserved.
//

#import "CBJViewController.h"
#import "cbjOneTableViewCell.h"
#import "cbjTwoTableViewCell.h"
#import "NetWorkHandker.h"
#import "shoujiModel.h"
#import "UIImageView+WebCache.h"
@interface CBJViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) UITableView *table1;
@property (nonatomic, retain) NSMutableArray *ModelArray;
@property (nonatomic, retain) NSMutableArray *KeyArray;
@property (nonatomic, retain) NSMutableDictionary *Dic;
@end

@implementation CBJViewController

- (void)dealloc {
    [_ModelArray release];
    [_KeyArray release];
    [_Dic release];
    [_table release];
    [_table1 release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithRed:12 / 255.0 green:118 / 255.0 blue:255 / 255.0 alpha:1];
//    创建假搜索
    [self JTF];
    [self creatTable];
    [self SjData];
}
- (void)creatTable {
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, 110, self.view.frame.size.height - 109) style:UITableViewStylePlain];
    _table.rowHeight = 41;
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.showsVerticalScrollIndicator = NO;
    _table.delegate = self;
    _table.dataSource = self;
   
    [_table registerNib:[UINib nibWithNibName:@"cbjOneTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    
    self.table1 = [[UITableView alloc] initWithFrame:CGRectMake(110, 60, 375 - 110, self.view.frame.size.height - 109) style:UITableViewStylePlain];
    _table1.rowHeight = 60;
    _table1.delegate = self;
    _table1.dataSource = self;
    _table1.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_table1 registerNib:[UINib nibWithNibName:@"cbjTwoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell1"];
    [self.view addSubview:_table1];
}
-(void)JTF {
    UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMake(10, 25, 355, 25)];
    TF.backgroundColor = [UIColor whiteColor];
    TF.placeholder = @"                 🔍请输入产品名称或型号";
    TF.layer.cornerRadius = 5;
    [self.view addSubview:TF];
    [TF release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _table) {
        return 20;
    }
    else if (tableView == _table1 && section > 2) {
        NSString *key = _KeyArray[section - 3];
        NSArray *arr = [_Dic valueForKey:key];
        return arr.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _table) {
        
        cbjOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        NSArray *arr = @[@"最近浏览", @"手机",@"笔记本电脑", @"平板电脑", @"超极本", @"相机摄像机", @"大家电", @"装机硬件", @"数码产品", @"硬件外设", @"台式整机", @"智能穿戴", @"厨卫电器", @"移动电源", @"无线路由器", @"办公打印", @"游戏机", @"掌上游戏机", @"车载导航", @"剃须刀", @"空气净化器", @"企业产品"];
        cell.oneLabel.text = arr[indexPath.row];
        cell.oneLabel.font = [UIFont systemFontOfSize:15];
        cell.oneLabel.textAlignment = NSTextAlignmentCenter;
        cell.tag = indexPath.row + 1000;
        cell.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:250 / 255.0 blue:250 / 255.0 alpha:1];
        return cell;
    }
    else if (tableView == _table1) {
        cbjTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];

        if (indexPath.section > 2) {
            NSString *key = _KeyArray[indexPath.section - 3];
            NSArray *arr = _Dic[key];
            cell.name.text = arr[indexPath.row][@"name"];
            [cell.tupian sd_setImageWithURL:[NSURL URLWithString:arr[indexPath.row][@"picUrl"]]];
            return cell;
        }
        else if (indexPath.section == 0) {
            cell.name.text = @"排行榜";
            return cell;
        }
        else if (indexPath.section == 1) {
            cell.name.text = @"筛选";
            return cell;
        }
        else if (indexPath.section == 2) {
            shoujiModel *model = _ModelArray[indexPath.row];
            cell.name.text = model.name;
            [cell.tupian sd_setImageWithURL:[NSURL URLWithString:model.picUrl]];
            return cell;
        }
        return nil;
    }
    return nil;
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSArray *array = @[@"排", @"筛", @"热"];
    [_KeyArray addObjectsFromArray:array];
    [_KeyArray sortUsingSelector:@selector(compare:)];
    return _KeyArray;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    cbjOneTableViewCell *cell = [_table viewWithTag:indexPath.row + 1000];
    cell.oneLabel.textColor = [UIColor colorWithRed:12 / 255.0 green:118 / 255.0 blue:255 / 255.0 alpha:1];
    cell.oneLabel.font = [UIFont systemFontOfSize:17];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.oneLabel.backgroundColor = [UIColor whiteColor];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    cbjOneTableViewCell *cell = [_table viewWithTag:indexPath.row + 1000];
    cell.oneLabel.textColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.oneLabel.font = [UIFont systemFontOfSize:15];
    cell.oneLabel.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:250 / 255.0 blue:250 / 255.0 alpha:1];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == _table1) {
        return 30;
    }
    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == _table1) {
        return _KeyArray.count;
    }
    return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"排行榜";
    }
    else if (section == 1) {
        return @"筛选";
    }
    else if (section == 2) {
        return @"热门品牌";
    }
    else if (section > 2) {
        NSString *str = _KeyArray[section - 3];
        return str;
    }
    return nil;
}

- (void)SjData {
    [NetWorkHandker GETRequestWithString:@"http://lib3.wap.zol.com.cn/index.php?c=Advanced_Manu_V1&subcateId=57" completion:^(id result) {
        NSData *data = result;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"rank"];
        self.ModelArray = [shoujiModel modelWithArray:array];
        
        //abc
        self.Dic = dic[@"abc"];
        self.KeyArray = [NSMutableArray arrayWithArray:_Dic.allKeys];
        [_KeyArray sortUsingSelector:@selector(compare:)];
        NSLog(@"%@", _KeyArray);
        [_table1 reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
